package com.example.hp.screenrose;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.Random;

/**
 * Created by zhangbin on 2018/6/28.
 */

public class RoseView extends SurfaceView implements SurfaceHolder.Callback {
    private final static String TAG = RoseView.class.getSimpleName() + "TAG";
    private int mWidth, mHeight;
    private SurfaceHolder mHolder = null;
    private Paint mTextPaint;
    int X_index = 0;
    int Y_index = 0;
    Random rand = new Random();
    private Canvas mCanvas;
    Resources res = getResources();
    private int[] picArrary = {R.drawable.rose0, R.drawable.rose1, R.drawable.rose2, R.drawable.rose3,
            R.drawable.rose4, R.drawable.rose5,};
    private int withSpace, heighSpace; //高宽中每个图片的间隔
    private int RoseCount, wCount, hCount; //画图片计数
    private final static int wRoseCount = 5; //横向画图片个数
    private final static int hRoseCount = 9; //纵向画图片个数
    private int[] indexList = new int[wRoseCount * hRoseCount]; //存放产生的随机坐标
    private Handler mHandler = new Handler();
    private final static int times = 500; //每张图出现的间隔时间，单位：毫秒
    private final static String[] mText = {"生", "日", "快", "乐", "！"}; //要显示的文字
    private String TextColer = "#f7931e"; //文字颜色

    public RoseView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mHolder = this.getHolder();
        mHolder.addCallback(this);
        mHolder.setFormat(PixelFormat.TRANSLUCENT);
        mTextPaint = new Paint();
        mCanvas = new Canvas();

        for (int i = 0; i < indexList.length; i++) {
            indexList[i] = i;
        }
        for (int i = 0; i < indexList.length; i++) {
            int t = rand.nextInt(indexList.length - 1) % (indexList.length - i) + i;
            int a = indexList[i];
            indexList[i] = indexList[t];
            indexList[t] = a;
            Log.w(TAG, "RoseView" + " = " + "random" + "  " + indexList[i]);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = measureWidth(widthMeasureSpec);
        mHeight = measureHeight(heightMeasureSpec);
        withSpace = mWidth / wRoseCount;
        heighSpace = mHeight / hRoseCount;
        setMeasuredDimension(mWidth, mHeight);
        Log.w(TAG, "RoseViewTag" + " = " + "onMeasure" + "  " + mWidth + "  " + mHeight + "  " + withSpace + "  " + heighSpace);
    }

    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = getWidth();
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.max(result, specSize);
            }
        }
        return result;
    }

    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = getHeight();
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
//            Log.w(TAG, "RoseViewTag" + " = " + "mRunnable" + "  " + mWidth + "  " + mHeight + "  " + withSpace + "  " + heighSpace);
            Bitmap bmp = BitmapFactory.decodeResource(res, picArrary[rand.nextInt(picArrary.length)]);
            Matrix matrix = new Matrix();
            matrix.postScale(withSpace / (float) bmp.getWidth(), heighSpace / (float) bmp.getHeight());
            bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
            Log.w(TAG, "RoseViewTag" + " = " + "RosePaint" + "  " + bmp.getWidth() + "  " + bmp.getHeight());

            X_index = (indexList[RoseCount] % wRoseCount) * withSpace;
            Y_index = (indexList[RoseCount] / wRoseCount) * heighSpace;

            mCanvas = mHolder.lockCanvas(new Rect(X_index, Y_index, X_index + bmp.getWidth(), Y_index + bmp.getHeight()));
            mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.ADD);
            mCanvas.drawBitmap(bmp, X_index, Y_index, null);
            mHolder.unlockCanvasAndPost(mCanvas);

            RoseCount++;
            if (RoseCount < wRoseCount * hRoseCount) {
                mHandler.postDelayed(mRunnable, times);
            } else {
                int textsize = mWidth / (mText.length + 2);
                mTextPaint = new Paint();
                mTextPaint.setColor(Color.parseColor(TextColer));
                mTextPaint.setStyle(Paint.Style.FILL);
                mTextPaint.setTextSize(textsize / 5 * 4);
                mTextPaint.setAlpha(255);
                mTextPaint.setAntiAlias(true);   //设置抗锯齿
                mTextPaint.setTextAlign(Paint.Align.CENTER);
                int X_text = 0;
                int Y_text = mHeight / 2;

                //去除首次画图遮挡下层图标
                mCanvas = mHolder.lockCanvas(new Rect(0, 0, 1, 1));
                mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.ADD);
                mCanvas.drawText("", 0, 0, mTextPaint);
                mHolder.unlockCanvasAndPost(mCanvas);

                try {
                    Thread.sleep(times);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                for (int i = 0; i < mText.length; i++) {
                    X_text = textsize / 2 * 4 + textsize * i;
                    mCanvas = mHolder.lockCanvas(new Rect(X_text - textsize / 2, Y_text - textsize, X_text + textsize / 2, Y_text + textsize / 3));
                    mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.ADD);
                    mCanvas.drawText(mText[i], X_text, Y_text, mTextPaint);
                    mHolder.unlockCanvasAndPost(mCanvas);
                    try {
                        Thread.sleep(times);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    };

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        RoseCount = 0;
        wCount = 0;
        hCount = 0;
        mHandler.postDelayed(mRunnable, 500);
        Log.w(TAG, "RoseViewTag" + " = " + "surfaceCreated");
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.w(TAG, "RoseViewTag" + " = " + "surfaceChanged");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mHandler.removeCallbacks(mRunnable);
        Log.w(TAG, "RoseViewTag" + " = " + "surfaceDestroyed");
    }
}
